import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
import { listEmployees, getEmployee, createEmployee, updateEmployee, deleteEmployee } from '../api/employee'

// 异步获取员工列表
export const fetchEmployees = createAsyncThunk(
  'employee/fetchEmployees',
  async (params, { rejectWithValue }) => {
    try {
      const response = await listEmployees(params)
      return response
    } catch (error) {
      return rejectWithValue(error.response?.data || { message: '获取员工列表失败' })
    }
  }
)

// 异步获取员工详情
export const fetchEmployeeDetail = createAsyncThunk(
  'employee/fetchEmployeeDetail',
  async (id, { rejectWithValue }) => {
    try {
      const response = await getEmployee(id)
      return response.data
    } catch (error) {
      return rejectWithValue(error.response?.data || { message: '获取员工详情失败' })
    }
  }
)

// 异步创建员工
export const addEmployee = createAsyncThunk(
  'employee/addEmployee',
  async (employeeData, { rejectWithValue }) => {
    try {
      const response = await createEmployee(employeeData)
      return response.data
    } catch (error) {
      return rejectWithValue(error.response?.data || { message: '添加员工失败' })
    }
  }
)

// 异步更新员工
export const editEmployee = createAsyncThunk(
  'employee/editEmployee',
  async ({ id, data }, { rejectWithValue }) => {
    try {
      const response = await updateEmployee(id, data)
      return response.data
    } catch (error) {
      return rejectWithValue(error.response?.data || { message: '更新员工失败' })
    }
  }
)

// 异步删除员工
export const removeEmployee = createAsyncThunk(
  'employee/removeEmployee',
  async (id, { rejectWithValue }) => {
    try {
      await deleteEmployee(id)
      return id
    } catch (error) {
      return rejectWithValue(error.response?.data || { message: '删除员工失败' })
    }
  }
)

const initialState = {
  employees: [],
  currentEmployee: null,
  pagination: {
    current: 1,
    pageSize: 10,
    total: 0
  },
  loading: false,
  error: null
}

const employeeSlice = createSlice({
  name: 'employee',
  initialState,
  reducers: {
    clearError: (state) => {
      state.error = null
    },
    setCurrentPage: (state, action) => {
      state.pagination.current = action.payload
    },
    setPageSize: (state, action) => {
      state.pagination.pageSize = action.payload
    },
    clearCurrentEmployee: (state) => {
      state.currentEmployee = null
    }
  },
  extraReducers: (builder) => {
    builder
      // 获取员工列表
      .addCase(fetchEmployees.pending, (state) => {
        state.loading = true
        state.error = null
      })
      .addCase(fetchEmployees.fulfilled, (state, action) => {
        state.loading = false
        state.employees = action.payload.data || []
        state.pagination.total = action.payload.total || 0
      })
      .addCase(fetchEmployees.rejected, (state, action) => {
        state.loading = false
        state.error = action.payload?.message || '获取员工列表失败'
      })
      // 获取员工详情
      .addCase(fetchEmployeeDetail.pending, (state) => {
        state.loading = true
        state.error = null
      })
      .addCase(fetchEmployeeDetail.fulfilled, (state, action) => {
        state.loading = false
        state.currentEmployee = action.payload
      })
      .addCase(fetchEmployeeDetail.rejected, (state, action) => {
        state.loading = false
        state.error = action.payload?.message || '获取员工详情失败'
      })
      // 添加员工
      .addCase(addEmployee.pending, (state) => {
        state.loading = true
        state.error = null
      })
      .addCase(addEmployee.fulfilled, (state) => {
        state.loading = false
      })
      .addCase(addEmployee.rejected, (state, action) => {
        state.loading = false
        state.error = action.payload?.message || '添加员工失败'
      })
      // 更新员工
      .addCase(editEmployee.pending, (state) => {
        state.loading = true
        state.error = null
      })
      .addCase(editEmployee.fulfilled, (state) => {
        state.loading = false
      })
      .addCase(editEmployee.rejected, (state, action) => {
        state.loading = false
        state.error = action.payload?.message || '更新员工失败'
      })
      // 删除员工
      .addCase(removeEmployee.pending, (state) => {
        state.loading = true
        state.error = null
      })
      .addCase(removeEmployee.fulfilled, (state, action) => {
        state.loading = false
        state.employees = state.employees.filter(employee => employee.id !== action.payload)
      })
      .addCase(removeEmployee.rejected, (state, action) => {
        state.loading = false
        state.error = action.payload?.message || '删除员工失败'
      })
  }
})

export const { clearError, setCurrentPage, setPageSize, clearCurrentEmployee } = employeeSlice.actions
export default employeeSlice.reducer